#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char* argv[]) {
  pid_t sid, pgid, tcid;
  pgid = atoi(argv[1]);

  /*
  if (setpgid(getpid(), pgid) < 0) {
    perror("setpgid");
    return -1;
  }
  */

  sid = getsid(getpid());
  tcid = tcgetpgrp(0);
  printf("pid = %d, pgid = %d, sid = %d, tcid = %d\n", getpid(), getpgid(getpid()), sid, tcid);

  /*
  sid = setsid();
  if (sid < 0) {
    perror("setsid");
  }
  */
  tcsetpgrp(0, pgid);
  tcid = tcgetpgrp(0);
  if (tcid < 0) {
    perror("tcgetpgrp");
  }
  printf("pid = %d, pgid = %d, sid = %d, tcid = %d\n", getpid(), getpgid(getpid()), sid, tcid);


  while(1) sleep(1);
  return 0;
}
